//
//  YWGraffitiView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/28.
//

#import "YWGraffitiView.h"
#import "MyBezierPath.h"

@interface YWGraffitiView()

/** 当前绘制的路径 */
@property (nonatomic, strong) UIBezierPath *path;

//保存当前绘制的所有路径
@property (nonatomic, strong) NSMutableArray *allPathArray;

//当前路径的线宽
@property (nonatomic, assign) CGFloat width;

//当前路径的颜色
@property (nonatomic, strong) UIColor *color;

/** 要绘制的图片 */
@property (nonatomic, strong) UIImage * ogrinalImage;


@end


@implementation YWGraffitiView


- (NSMutableArray *)allPathArray {
    
    if (_allPathArray == nil) {
        _allPathArray = [NSMutableArray array];
    }
    return _allPathArray;
}
//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    
    //添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
    
    self.width = 3;
    self.color = [UIColor redColor];
    
}


//设置初始图片颜色
- (void)setFirstImage:(UIImage *)image{
    _ogrinalImage = image;
    self.allPathArray = [NSMutableArray arrayWithArray:@[image]];
    [self setNeedsDisplay];
}


//重置图层
-(void)removeAllHandle{
    if(self.ogrinalImage != nil) {
        self.allPathArray = [NSMutableArray arrayWithArray:@[self.ogrinalImage]];
        [self setNeedsDisplay];
    }
    
}


//撤销图层
-(void)previousHandle{
    
    if(self.allPathArray.count > 1) {
        //删除最后一个路径
        [self.allPathArray removeLastObject];
        //重绘
        [self setNeedsDisplay];
    }
    
}

//设置线的宽度
- (void)setLineWith:(CGFloat)lineWidth{
    self.width = lineWidth;
}


//设置线的颜色
- (void)setLineColor:(UIColor *)color{
    self.color = color;
}


//获取当前图层
-(UIImage *)getCurrentLayer{
    
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}



- (void)pan:(UIPanGestureRecognizer *)pan {
    
    //获取的当前手指的点
    CGPoint curP = [pan locationInView:self];
    //判断手势的状态
    if(pan.state == UIGestureRecognizerStateBegan) {
        //创建路径
        MyBezierPath *path = [[MyBezierPath alloc] init];
        self.path = path;
        //设置起点
        [path moveToPoint:curP];
        
        //设置线的宽度
        [path setLineWidth:self.width];
        //设置线的颜色
        //什么情况下自定义类:当发现系统原始的功能,没有办法瞒足自己需求时,这个时候,要自定义类.继承系统原来的东西.再去添加属性自己的东西.
        path.color = self.color;
        
        [self.allPathArray addObject:path];
        
    } else if(pan.state == UIGestureRecognizerStateChanged) {
        
        //绘制一根线到当前手指所在的点
        [self.path addLineToPoint:curP];
        //重绘
        [self setNeedsDisplay];
    }
    
}

-(void)drawRect:(CGRect)rect {
    
    //绘制保存的所有路径
    for (MyBezierPath *path in self.allPathArray) {
        //判断取出的路径真实类型
        if([path isKindOfClass:[UIImage class]]) {
            UIImage *image = (UIImage *)path;
            [image drawInRect:rect];
        }else {
            path.lineCapStyle = kCGLineCapRound;
            path.lineJoinStyle = kCGLineJoinRound;
            [path.color set];
            [path stroke];
            
        }
      
    }
    
}



@end
